$(document).ready(function(){   
       //inicializa tabla de datos 
       tabla();          
       
       $('#requerimientosindex-dt tbody').on('dblclick', 'tr', function () {
            var id = $('td', this).eq(0).text();            
            location.href = "/xmlc2/ControlServlet?accion=requerimiento.jsp?idrx="+id;
        } );
       
       $("#salir").click(salir);
       $(".ui-autocomplete").css("z-index","2000");
       
       


       
});

function grafica(){
   
}


function tabla() {
    // Setup - add a text input to each footer cell
    var i=0;
    $('#requerimientosindex-dt tfoot th').each(function() {
        var title = $('#requerimientosindex-dt thead th').eq($(this).index()).text();
        $(this).html('<input id="b'+i+'" type="text" size="10" placeholder="Buscar ' + title + '" />');
        i++;
    });

    var table = $('#requerimientosindex-dt').DataTable({
        dom: 'T<"clear">lfrtip',
        processing: true,
        serverSide: true,
        "bDestroy": true,
        "order": [[0, "desc"]],
        ajax: {
            url: "/xmlc2/ListaRequerimientosInicioServlet",
            type: "POST"
        },
        columns: [
            {data: "id"},
            {data: "codigo"},
            {data: "titulo"},
            {data: "estado"},
            {data: "inicio"},
            {data: "fin"},
            {data: "id_reponsable"},
            {data: "id_tipo"}
        ],
        "fnRowCallback": function( nRow, aData ) {

            var $nRow = $(nRow);

            switch(parseInt(aData.id_tipo)){
                    case 1:
                        //req
                        $nRow.css({"background-color":"#00FF80"});
                    break;

                    case 2:
                        //mejora
                        $nRow.css({"background-color":"#B5F06E"});
                    break;

                    case 3:
                        //tarea
                        $nRow.css({"background-color":"#F7D358"});
                    break;

                    case 4:
                        //error
                        $nRow.css({"background-color":"#F7B0A6"});
                    break;

                }
                return nRow
              }
    });

    // Apply the filter
    $("#requerimientosindex-dt tfoot input").on('keyup change', function() {
        table
                .column($(this).parent().index() + ':visible')
                .search(this.value)
                .draw();
    });
    
    $("#b6").hide();
    
     $("#buscar").autocomplete({
        source: "/xmlc2/ListaBuscadorServlet",
        minLength: 2,
        select: function( event, ui ) {
            $(this).val("");
            location.href = "/xmlc2/ControlServlet?accion=requerimiento.jsp?idrx="+ui.item.id;    
        }
    });
    
    
}


function salir(){
    location.href = "/xmlc2/LogoutServlet";
}